package com.sanji.sjmallmodule.account;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallutils.Tools;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的积分
 **/
public class MyScoreActivity extends BaseActivity implements View.OnClickListener, NoHttpListener {
    @BindView(R.id.common_left_tv)
    TextView commonLeftTv;
    @BindView(R.id.my_score_show_number)
    TextView myScoreShowNumber;//显示积分
    @BindView(R.id.common_back_img)
    ImageView commonBackImg;
    @BindView(R.id.activity_my_score)
    RelativeLayout scoreRelative;
    private Request<JSONObject> request;
    private String userId;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_score);
        ButterKnife.bind(this);
        initView();
        requestScore();
    }

    public void initView() {
        commonLeftTv.setText(getString(R.string.my_score));
        commonBackImg.setOnClickListener(this);
        userId = myShare.getString(ConfigConts.USER_ID, null);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
        }
    }

    /**
     * 网络请求积分
     */
    public void requestScore() {
        url = ConfigAPI.SCORE_URL + userId;
        request = NoHttp.createJsonObjectRequest(url, RequestMethod.GET);
        //设置取消标志
        request.setCancelSign(url.hashCode());
        NoHttpCallBack.getInstance().add(mCtx, url.hashCode(), request, this, true, true);
    }

    @Override
    public void onSuccessed(int what, Response response) {
        if (what == url.hashCode()) {
            JSONObject object = new JSONObject();
            object = (JSONObject) response.get();
            String status = object.optString(ConfigConts.STATUS_STRING);
            if (status.equals(ConfigConts.STATUS_SUCCESS)) {
                scoreRelative.setVisibility(View.VISIBLE);
                try {
                    Object resesultObj = object.get("result");
                    myScoreShowNumber.setText(resesultObj.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                String errorMsg = object.optString(ConfigConts.ERRORMSG);
                Tools.toastTools(mCtx, errorMsg);
            }

        }
    }

    @Override
    public void onFailed(int what, Response response) {
        switch (what) {
            case 0:
                Tools.toastTools(mCtx,getString(R.string.false_message));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != request) {
            request.cancel();
        }
    }
}
